<?php
namespace app\index\controller;

use container\dependency\Car;
use container\dependency\Person;
use container\reflection\TestReflection;
use think\facade\Config;
use think\Controller;
use test\TestArrayAccess;
use container\RegistrationTree;

class Index extends Controller
{
    public function index ()
    {
        return '我是index方法';
        // echo '我是index方法';
    }

    public function _empty ()
    {
        echo '执行的方法不存在时会执行我';
    }

    /**
     * User : 咔咔
     * Notes: 路由方法测试
     * Time :2020/10/31 16:20
     */
    public function hello($name = '')
    {
        echo '测试路由方法'.'参数为'.$name;
    }

    public function kaka($name = '')
    {
        $data = Config::get('app.');
        // dump($data);die;
        echo '测试路由的return功能'.$name;
    }

    /**
     * author:咔咔
     * 反射机制
     * @route('reflection');
     */
    public function reflection ()
    {
        $obj = new TestReflection();

        $obj2 = new \ReflectionClass($obj);
        // 获取这个类的所有方法
        $methods = $obj2->getMethods();
        // 获取这个类的所有属性
        $getProperties = $obj2->getProperties();
        // 相当于实例化这个类
        $instance = $obj2->newInstance();
        // $instance->getName();

        // 带参数的方法调用
        $method = $obj2->getMethod('getKaka');
        // 1---2
        $method->invokeArgs($instance,[1,2]);

        // 不带参数的方法调用
        $method = $obj2->getMethod('getName');
        // 咔咔
        $method->invoke($instance);
        // print_r($getProperties);
        // print_r($methods);die;
    }
    
    /**
     * author:咔咔
     * 依赖注入测试
     * @route('dependency');
     */
    public function dependency ()
    {
        $person = new Person();
        $obj = new Car();
        var_dump($person->buy($obj));die;
    }

    /**
     * author:咔咔
     * 注册树模式
     * @route('registrationTree');
     */
    public function registrationTree ()
    {
        $object = new \container\TestTree();
        RegistrationTree::set('kaka',$object);
        $data = RegistrationTree::get('kaka')->getTreeContent();
        dump($data);die;
    }
    
    /**
     * User : 咔咔
     * Notes: 单例模式
     * Time :2020/9/20 21:41
     * @route('getInstance');
     */
    public function getInstance()
    {
        $single = \container\Single::getInstance()->getName();
        \container\Single::getInstance()->getName();
        \container\Single::getInstance()->getName();
        \container\Single::getInstance()->getName();
    }
    
    /**
     * User : 咔咔
     * Notes:
     * Time :2020/9/20 18:40
     * @route('getConfig')
     */
    public function getConfig()
    {
        $name = 'app_name';
        $default = null;
        if ($name && false === strpos($name, '.')) {
            $name = 'app'. '.' . $name;
        }
        $name    = explode('.', $name);
        $name[0] = strtolower($name[0]);
        $config  = \Config::get();

        /**
         * $name最终的值
        Array
        (
            [0] => app
            [1] => app_name
        )
         */
        // 按.拆分成多维数组进行判断
        foreach ($name as $val) {
            if (isset($config[$val])) {
                $config = $config[$val];
            } else {
                return $default;
            }
        }
        return $config;
    }

    /**
     * User : 咔咔
     * Notes:
     * Time :2020/9/20 18:22
     * @route('testConfig')
     */
    public function testConfig ()
    {
        $data = Config::get('app_name');
        print_r($data);
    }
    
    /**
     * author:咔咔
     */
    public function testYaml ()
    {
        $data = \Config::get('kaka.');
        print_r($data);die;
    }
    
    /**
     * author:咔咔
     * @route('testYaconf');
     */
    public function testYaconf ()
    {
        dump(phpinfo());die;
        $array = [1,2,3];
        dump(end($array));die;
        // $title = \Yaconf::get('kaka.name');
        $weChat = \Yaconf::get('kaka');
        $check = \Yaconf::has('kaka.name');
        dump($check);die;
    }
    /**
     * User : 咔咔
     * Notes: 测试ArrayAccess
     * Time :2020/9/16 21:02
     * @route('testArrayAccess')
     */
    public function testArrayAccess()
    {
        $obj = new TestArrayAccess();
        // $exist = $obj->offsetExists('title');
        // $getData = $obj->offsetGet('title');
        // dump($exist);
        // dump($getData);die;
        dump($obj['title']);die;
    }

    // public function index()
    // {
    //     $a = 123;
    //     // Kaka::say();
    //     // echo '咔咔微信：fangkangfk    微信公众号：PHP初学者必看';
    // }

    /**
     * User : 咔咔
     * Notes:
     * Time :2020/9/17 23:15
     * @route('kaka');
     */
    public function testLoad()
    {
        $data = ['kaka','kaka2','kaka3'];
        foreach ($data as $k => $v) {
            $this->testNull($v,$k);
        }
    }

    public function testNull($v,$k=null)
    {
        dump($k);
    }

    /**
     * User : 咔咔
     * Notes:
     * Time :2020/9/18 0:13
     * @route('ini')
     */
    public function getIni()
    {
        $res = parse_ini_file('D:/phpstudy_pro/WWW/ThinkPHPSourceCodeAnalysis/config/test.ini');
        dump($res);die;
    }
}
